#include "Cube.h"

Cube::Cube()
{
    //初始化坐标
    x = 0;
    y = 0;
    
    //初始化速率
    xVel = 0;
    yVel = 0;
}



void Cube::handle_input1(SDL_Event event)
{
    //当一个键被按下
    if( event.type == SDL_KEYDOWN )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_w)
        {
            yVel -= CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_s)
        {
            yVel += CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_a)
        {
            xVel -= CUBE_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_d)
        {
            xVel += CUBE_WIDTH / 2;
        }
    }
    //当一个键盘按键被松开
    else if( event.type == SDL_KEYUP )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_w)
        {
            yVel += CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_s)
        {
            yVel -= CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_a)
        {
            xVel += CUBE_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_d)
        {
            xVel -= CUBE_WIDTH / 2;
        }
    }
}


void Cube::handle_input(SDL_Event event)
{
    //当一个键被按下
    if( event.type == SDL_KEYDOWN )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_UP)
        {
            yVel -= CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_DOWN)
        {
            yVel += CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_LEFT)
        {
            xVel -= CUBE_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_RIGHT)
        {
            xVel += CUBE_WIDTH / 2;
        }
    }
    //当一个键盘按键被松开
    else if( event.type == SDL_KEYUP )
    {
        //调整速度
        if(event.key.keysym.sym == SDLK_UP)
        {
            yVel += CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_DOWN)
        {
            yVel -= CUBE_HEIGHT / 2;
        }
        else if(event.key.keysym.sym == SDLK_LEFT)
        {
            xVel += CUBE_WIDTH / 2;
        }
        else if(event.key.keysym.sym == SDLK_RIGHT)
        {
            xVel -= CUBE_WIDTH / 2;
        }
    }
}


void Cube::move()
{
    //将圆点左右移动
    x += xVel;
    //将圆点上下移动
    y += yVel;
}

void Cube::show(SDL_Surface *screen)
{
    SDL_Rect offset;
    offset.x = x;
    offset.y = y;

    SDL_BlitSurface( background, NULL, screen, &offset );
}


void Cube::setBackground(SDL_Surface *background)
{
    this->background = background;
}


void Cube::setPos(int x, int y)
{
    this->x = x;
    this->y = y;
}
void Cube::getPos(int &x, int &y)
{
    x = this->x;
    y = this->y;
}


void Cube::adaptPos(int w, int h)
{
    if( this->x < 0 )
    {
        this->x = 0;
    }
    else if( this->x > (w-CUBE_WIDTH) )
    {
        this->x = w-CUBE_WIDTH;
    }

    if( this->y < 0 )
    {
        this->y = 0;
    }
    else if( this->y > (h-CUBE_HEIGHT) )
    {
        this->y = h-CUBE_HEIGHT;
    }
}
